%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%   BornAgain User Manual
%%
%%   homepage:   http://www.bornagainproject.org
%%
%%   copyright:  Forschungszentrum Jülich GmbH 2015
%%
%%   license:    Creative Commons CC-BY-SA
%%
%%   authors:    Scientific Computing Group at MLZ Garching
%%               C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{ifdraft}

\let\angstrom=\AA

%-------------------------------------------------------------------------------
%  Page layout
%-------------------------------------------------------------------------------

% Horizontal setup
\textwidth=410pt
\hoffset=210mm % width of A4
\advance\hoffset by -1\textwidth
\ifdraft{\hoffset=0.\hoffset}{\hoffset=0.5\hoffset}
\advance\hoffset by -1in
% Now a slight assymmetry to leave more blank on the side of the fold
\ifdraft{}{
  \evensidemargin=0pt
  \oddsidemargin=5pt
  \advance\evensidemargin by -1\oddsidemargin}

\def\myparindent{5ex}
\setlength{\parindent}{\myparindent} % workaround, for colorboxes

% Vertical setup
\setlength{\headheight}{0pt}
\setlength{\headsep}{10pt}
\setlength{\textheight}{630pt} % default=592pt
\setlength{\footskip}{45pt}
\setlength{\marginparwidth}{7em}
\renewcommand{\baselinestretch}{1.02}

\renewcommand{\arraystretch}{1.3}

%-------------------------------------------------------------------------------
%  Symbols, fonts
%-------------------------------------------------------------------------------

\usepackage{amsmath}
\usepackage{mathtools} % has \coloneqq for :=
% \usepackage{manfnt} % for \dbend
\usepackage{dingbat}
\usepackage{amssymb}
\usepackage{braket}

% prevent unicode-math from overwriting

\AtBeginDocument{\renewcommand{\Re}{\operatorname{Re}}}
\AtBeginDocument{\renewcommand{\Im}{\operatorname{Im}}}

% Math operators
\DeclareMathOperator{\sinc}{sinc}
\DeclareMathOperator{\expmone}{expm1}
\DeclareMathOperator{\expmtwo}{expm2}
\DeclareMathOperator{\Tr}{Tr}

%-------------------------------------------------------------------------------
%  Footer
%-------------------------------------------------------------------------------

\makeatletter
\def\@oddfoot{{v \version}\phantom{\thepage}\hfill BornAgain Form Factor Catalog\hfill
\phantom{v \version}\thepage}
\def\@evenfoot{\@oddfoot}
\makeatother
\def\pagereset{}

%-------------------------------------------------------------------------------
%  Sectioning
%-------------------------------------------------------------------------------

\makeatletter

\newif\ifnumberedchapter
\def\@makechapterhead#1{\numberedchaptertrue\mychapterhead{#1}}
\def\@makeschapterhead#1{\numberedchapterfalse\mychapterhead{#1}}

\newif\iffirstchapterinpart
\firstchapterinpartfalse
\renewcommand\part{%
  \clearpage\firstchapterinparttrue
  %\thispagestyle{plain}%
  \if@twocolumn
    \onecolumn
    \@tempswatrue
  \else
    \@tempswafalse
  \fi
  \vspace*{50\p@ plus 10\p@ minus 10\p@}%
  \secdef\@part\@spart}


\def\@part[#1]#2{%
    \refstepcounter{part}%
    \addcontentsline{toc}{part}{Part~\thepart\hspace{1em}#1}%
    \markboth{}{}%
    {\interlinepenalty \@M
     \normalfont
     \ifnum \c@secnumdepth >-2\relax
       \parindent \z@ \Large\bfseries \partname\nobreakspace\thepart
       \par
       \vskip 20\p@
     \fi
     \parindent \z@ \huge \bfseries #2\par}
    }

\newif\iffirstsectioninchapter
\def\sectionclear{\iffirstsectioninchapter\else\ifdraft{\clearpage}{}\fi}

\renewcommand\chapter{
  \firstsectioninchaptertrue
  \pagereset
  \iffirstchapterinpart\else\clearpage\fi
  \firstchapterinpartfalse
  %\thispagestyle{myheadings}
  %                  \thispagestyle{plain}%
  \global\@topnum\z@
  \@afterindentfalse
  \secdef\@chapter\@schapter}

\def\mychapterhead#1{%
  \vspace*{50\p@ plus 10\p@ minus 10\p@}%
  {\parindent \z@ \normalfont
    \raggedright
    \LARGE \bfseries \ifnumberedchapter\thechapter~~\fi #1\par\nobreak
    \interlinepenalty\@M
    \vskip 10\p@ plus 2\p@ minus 2\p@
%    \hrule
    \interlinepenalty\@M
    \vskip 40\p@ plus 8\p@ minus 8\p@
  }}

\def\pagemode#1{%
\ifdraft{
\def\thepage{#1:\arabic{page}} % compositor character must agree with page_compositor in *.ist files
\def\pagereset{\setcounter{page}{1}}}{}}

% Index, Bibliography, ...
\def\otherchapter#1#2{
  \clearpage
  \pagemode{#2}
  \phantomsection
  \addcontentsline{toc}{chapter}{#1}
  \markboth{#1}{#1}}

\def\ichapter#1{\chapter*{#1}\addcontentsline{toc}{chapter}{#1}}
\def\isection#1{\section*{#1}\addcontentsline{toc}{section}{#1}}

\renewcommand\section{\@startsection{section}{1}{\z@}
  {-3.5ex \@plus -1.5ex \@minus -.5ex}
  {2.3ex \@plus .8ex \@minus .5ex}
  {\sectionclear\global\firstsectioninchapterfalse\normalfont\Large\bfseries}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}
  {-3.25ex\@plus -1.3ex \@minus -.4ex}
  {1.5ex \@plus .5ex \@minus .3ex}
  {\normalfont\large\bfseries}}

% from size11.clo
\renewcommand\normalsize{
   \@setfontsize\normalsize\@xipt{13.6}%
   \abovedisplayskip 11\p@ \@plus7\p@ \@minus6\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6.5\p@ \@plus3.5\p@ \@minus3\p@
   \belowdisplayskip \abovedisplayskip
   \let\@listi\@listI}
\makeatother

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{2}
%\usepackage[toc,page]{appendix}
\usepackage{titlesec}

\def\TTI#1{#1@\Code{#1}}
\def\ttIdx#1{\texttt{#1}\index{\TTI{#1}}}
\def\ttIdx#1{\texttt{#1}\index{\TTI{#1}}}
\def\clFctHide#1#2{\index{\TTI{#1}!\TTI{#2}}\index{#2 #1@\Code{#2} (\Code{#1})}}
\def\clFct#1#2{\texttt{#2}\clFctHide{#1}{#2}}
\def\constrHide#1{\index{\TTI{#1}!constructor}}
\def\constr#1{\texttt{#1}\constrHide{#1}}

%-------------------------------------------------------------------------------
%  Table of Contents
%-------------------------------------------------------------------------------

% from latex.ltx, worked incorrectly for non-standard page label
\makeatletter
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hbox{\hfil\normalfont \normalcolor #5}% % patched here
     \par}%
  \fi}
\makeatother

%-------------------------------------------------------------------------------
%  Index, List of Symbols
%-------------------------------------------------------------------------------

\ifdraft{\usepackage{showidx}}{}
\usepackage[noautomatic]{imakeidx}
\makeindex

\makeatletter
% patch showidx
\def\@showidx#1{%
  \insert\indexbox{\tiny
    \hsize2\marginparwidth
    \hangindent\marginparsep \parindent\z@
    \everypar{}\let\par\@@par \parfillskip\@flushglue
    \lineskip\normallineskip
    \baselineskip .8\normalbaselineskip\sloppy
    \raggedright \leavevmode
    \vrule \@height .7\normalbaselineskip \@width \z@\relax
        #1\relax
    \vrule \@height \z@ \@depth .3\normalbaselineskip \@width \z@}}
\def\@mkidx{\smash{\hbox{\raise3cm\vbox to \z@{\hbox{\@rightidx\box\indexbox}\vss}}}}
\def\@rightidx{\hskip\columnwidth \hskip\marginparsep}
  \renewenvironment{theindex}
    {\imki@maybeaddtotoc
     \imki@indexlevel{\indexname}\imki@indexheaders
     % patched here \thispagestyle{\imki@firstpagestyle}%
     \ifnum\imki@columns>\@ne
       \columnsep \imki@columnsep
       \ifx\imki@idxprologue\relax
         \begin{multicols}{\imki@columns}
       \else
         \begin{multicols}{\imki@columns}[\imki@idxprologue]
       \fi
     \else
       \imki@idxprologue
     \fi
     \global\let\imki@idxprologue\relax
     \parindent\z@
     \parskip\z@ \@plus .3\p@\relax
     \columnseprule \ifKV@imki@columnseprule.4\p@\else\z@\fi
     \raggedright
     \let\item\@idxitem
     \imki@othercode}
    {\ifnum\imki@columns>\@ne\end{multicols}\fi}
\makeatother


\usepackage[refpage]{nomencl}
\makenomenclature
\renewcommand{\nomname}{List of Symbols}
  % see nomencl.txt for how to force the ordering of symbols
\def\nompageref#1{,~\hyperpage{#1}\nomentryend\endgroup}
\makeatletter
\def\thenomenclature{%
  \nompreamble
  \list{}{%
    \labelwidth\nom@tempdim
    \leftmargin\labelwidth
    \advance\leftmargin\labelsep
    \itemsep\nomitemsep
    \let\makelabel\nomlabel}}
\makeatother

%-------------------------------------------------------------------------------
%  Improve LaTeX basics
%-------------------------------------------------------------------------------

\usepackage{enumitem}
\usepackage{subfigure}

\usepackage{placeins} % defines \FloatBarrier
\usepackage{float}
\usepackage[font={small}]{caption}

%-------------------------------------------------------------------------------
%  Tables, code listings, ...
%-------------------------------------------------------------------------------

\usepackage{longtable}
%\usepackage{booktabs} % defines \toprule &c for use in tabular
% see http://tex.stackexchange.com/questions/78075/multi-page-with-tabulary
\usepackage{tabulary}

\usepackage[final]{listings}
\usepackage[x11names]{xcolor}
\usepackage{lstcustom} % under our control
\renewcommand{\lstfontfamily}{\ttfamily}

\def\setPy{\lstset{language=python,style=eclipseboxed,numbers=none,nolol,
   backgroundcolor=\color{SlateGray1}}}
\def\setPyNum{\lstset{language=python,style=eclipseboxed,nolol,
   backgroundcolor=\color{SlateGray1}}}
\def\setCpp{\lstset{language=python,style=eclipseboxed,numbers=none,nolol,
   backgroundcolor=\color{Wheat1}}}
\setPy

%-------------------------------------------------------------------------------
%  Tikz pictures
%-------------------------------------------------------------------------------

\usepackage{tikz}
%\usepackage{tikz-uml}
\usetikzlibrary{trees,matrix,positioning,decorations.pathreplacing,calc}

\newcommand{\ntikzmark}[2]
           {#2\thinspace\tikz[overlay,remember picture,baseline=(#1.base)]
             {\node[inner sep=0pt] (#1) {};}}

\newcommand{\makebrace}[3]{%
    \begin{tikzpicture}[overlay, remember picture]
        \draw [decoration={brace,amplitude=0.6em},decorate]
        let \p1=(#1), \p2=(#2) in
        ({max(\x1,\x2)}, {\y1+1.5em}) -- node[right=0.6em] {#3} ({max(\x1,\x2)}, {\y2});
    \end{tikzpicture}
}

%-------------------------------------------------------------------------------
%  Conditional pictures
%-------------------------------------------------------------------------------

\def\TW{\textwidth}
\ifdraft{\def\skipresult{true}}{\def\skipresult{false}}
\def\includefinal#1#2{\includegraphics[width=#1,draft=\skipresult]{#2}}

%-------------------------------------------------------------------------------
%  Highlighting
%-------------------------------------------------------------------------------

\usepackage{mdframed}
\input FixMdframed % bug fix to prevent erroneous page breaks
% doesnt work:
%\newcommand\widow{%
%  \widowpenalty=10000
%
%  \widowpenalty=150}

\def\defineBox#1#2#3#4#5{
  \newmdenv[
    usetwoside=false,
    skipabove=3pt minus 1pt plus 3pt,
    skipbelow=3pt minus 1pt plus 3pt,
    leftmargin=-4pt,
    rightmargin=-4pt,
    innerleftmargin=2pt,
    innerrightmargin=2pt,
    innertopmargin=4pt,
    innerbottommargin=4pt,
    backgroundcolor=#3,
    topline=false,
    bottomline=false,
    linecolor=#4,
    linewidth=2pt,
    ]{#2*}
  \newenvironment{#1}
    {\begin{#2*}\makebox[0pt][r]{\smash{#5}}\ignorespaces}
    {\end{#2*}\mdbreakon}
}

\def\mdbreakoff{\makeatletter\booltrue{mdf@nobreak}\makeatother}
\def\mdbreakon{\makeatletter\boolfalse{mdf@nobreak}\makeatother}

\def\marginSymbolLarge#1#2{\raisebox{-4ex}{\includegraphics[width=3em]{#1}\hspace{10pt}}}
\def\marginSymbolMedium#1#2{\raisebox{-2ex}{\includegraphics[width=2em]{#1}\hspace{15pt}}}

\def\MissingSection{\begin{boxWork}\ldots\ to be written \ldots\end{boxWork}}

%-------------------------------------------------------------------------------
%  Hyper ref and clever ref
%-------------------------------------------------------------------------------

\usepackage[final,pagebackref=true]{hyperref} % wants to be included last
\hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black},
    pdftitle={BornAgain Form Factor Catalog} % seems to be ignored
}
\ifdraft{\usepackage[right]{showlabels}}{}

\usepackage{cleveref}

\crefformat{equation}{(#2#1#3)}
\Crefformat{equation}{Equation~(#2#1#3)}
\crefmultiformat{equation}{(#2#1#3)}{ and~(#2#1#3)}{, (#2#1#3)}{ and~(#2#1#3)}
\crefrangeformat{equation}{(#3#1#4) to~(#5#2#6)}

\crefformat{part}{Part~#2#1#3}
\Crefformat{part}{Part~#2#1#3}

\crefformat{chapter}{Chapter~#2#1#3}
\Crefformat{chapter}{Chapter~#2#1#3}

\crefformat{section}{Sec.~#2#1#3}
\Crefformat{section}{Section~#2#1#3}
\crefmultiformat{section}{Secs.\ #2#1#3}{ and~#2#1#3}{, #2#1#3}{ and~#2#1#3}

\crefformat{subsection}{Sec.~#2#1#3}
\Crefformat{subsection}{Section~#2#1#3}

\crefformat{subsubsection}{Sec.~#2#1#3}
\Crefformat{subsubsection}{Section~#2#1#3}

\crefformat{figure}{Fig.~#2#1#3}
\Crefformat{figure}{Figure~#2#1#3}
